<template>
  <el-dialog
    ref="passwordDialog"
    title="确认密码"
    width="400px"
    :close-on-click-modal="false"
    :visible.sync="params.visible"
  >
    <el-form v-model="formData">
      <el-form-item :label="formData.label">
        <el-input ref="pwd" v-model="formData.password" type="password" />
      </el-form-item>
    </el-form>
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
import { test2 } from '@/api/base'
import { ParamArr } from '../../../public/js/paramarr'
import Global from '../../../public/js/global'
import { hex_md5 } from '../../../public/js/md5'
import { webUserVerify } from '@/api/user'
import DialogBtn from './DialogBtn.vue'
export default {
  name: 'PasswordDialog',
  components: { DialogBtn },
  props: {
    params: {
      type: Object,
      default() {
        return {
          visible: false
        }
      }
    }
  },
  data() {
    return {
      visible: false,
      formData: {
        label: '',
        password: ''
      },
      key: ''
    }
  },
  mounted() {
    this.init()
    this.$nextTick(() => {
      this.$refs.pwd.focus()
    })
  },
  methods: {
    init() {
      this.$showLoading(
        '正在加载',
        this.$refs.passwordDialog.$el.querySelector('div')
      )
      this.formData.label = `请输入"${this.$store.state.user.userID}[${this.$store.state.user.userName}]"的密码`
      test2(new ParamArr())
        .then((vPar) => {
          this.key = vPar.getStr('key')
        })
        .finally(() => {
          this.$closeLoading()
        })
    },
    confirm() {
      if (this.formData.password.trim() === '') {
        return this.$message.warning('密码不能为空，请输入')
      }
      const vPar = new ParamArr()
      vPar.setStr(
        'Pass',
        Global.encryptByDES(hex_md5(this.formData.password), this.key)
      )
      vPar.setStr(
        'PassDES',
        Global.encryptByDES(this.formData.password, this.key)
      )
      webUserVerify(vPar)
        .then(() => {
          if (this.params.confirm !== undefined) {
            this.params.confirm()
          } else {
            this.$emit('confirm', '')
          }
        })
        .catch(() => {
          this.$message.error('密码错误')
        })
    }
  }
}
</script>

<style scoped>
</style>
